import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gold/base/config.dart';
import 'package:gold/model/gold_recover_order_model.dart';
import 'package:gold/route/routes.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_extension.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/utils/xfg_utils.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';

/// @description: 黄金回收线上订单详情
/// @author tangzhentao
/// @created at 18:56 2025/1/19

class OnlineDetailPage extends StatelessWidget {
  /// 订单详情
  final GoldRecoverOrderModel order;

  const OnlineDetailPage({super.key, required this.order});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: xfgAppbar(title: '详情'),
      body: Padding(
        padding: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size10, XfgSize().size10, XfgSize().bottomBarHeight == 0 ?  XfgSize().size24:XfgSize().bottomBarHeight),
        child: Column(
          children: [
            Expanded(
              child: Container(
                decoration: white6Decoration,
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      /// 价格信息
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: XfgSize().size10, vertical: XfgSize().size15),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            XfgText('黄金料${order.weight?? 0.0}克', fontSize: XfgSize().text18, color: XfgColor.text1D, bold: FontWeight.bold),
                            XfgText(order.auditTotalPrice != null ? '到手 ¥${order.auditTotalPrice!.toStringAsFixed(2)}':'预估 ¥${order.totalPrice?.toStringAsFixed(2)?? '0.00'}',
                                fontSize: XfgSize().text14, color: XfgColor.theme)
                          ],
                        ),
                      ),
                      strokeHeightContainer,
                      _itemCopyWidget('订单编号', '11234111'),
                      _itemWidget('交易快照', '发生争议时，可作为依据'),
                      _itemWidget('下单时间', order.createdAt?? '-'),
                      _itemWidget('发货时间', order.shipmentAt?? '--'),
                      _itemWidget('签收时间', order.receiptAt?? '--'),
                      _itemWidget('核件时间', order.auditAt?? '--'),
                      _itemWidget('确认交易时间', order.confirmAt?? '--'),
                      strokeHeightContainer,
                      _itemWidget('配送方式', '京东快递'),
                      _itemWidget('收货地址', '地址值地址地址值地址地址值地址'),
                    ],
                  ),
                ),
              ),
            ),
            SizedBox(height: XfgSize().size25),
            Row(mainAxisAlignment: MainAxisAlignment.end, children: _getButtons())
          ],
        ),
      ),
    );
  }

  /// 获取按钮集合
  List<Widget> _getButtons() {
    final List<Widget> buttons = [];
    if (order.status == RecoverOrderStatus.unReceipt || order.status == RecoverOrderStatus.unVerify ||
        order.status == RecoverOrderStatus.unConfirm || order.status == RecoverOrderStatus.done) {
      buttons.addAll([
        TextButton(
            onPressed: () => _checkLogistics(true),
            style: _buttonStyle(XfgColor.colorF2),
            child: XfgText('发件物流', fontSize: XfgSize().text14, color: XfgColor.text1D, bold: FontWeight.bold)
        ),
        SizedBox(width: XfgSize().size10),
      ]);
    }
    if (order.status == RecoverOrderStatus.canceled) {
      buttons.addAll([
        TextButton(
            onPressed: () => _checkLogistics(false),
            style: _buttonStyle(XfgColor.colorF2),
            child: XfgText('退件物流', fontSize: XfgSize().text14, color: XfgColor.text1D, bold: FontWeight.bold)
        ),
        SizedBox(width: XfgSize().size10),
      ]);
    }
    buttons.addAll([
      TextButton(
          onPressed: () => toGoldRecovery(),
          style: _buttonStyle(XfgColor.theme),
          child: XfgText('再次回收', fontSize: XfgSize().text14, color: XfgColor.white, bold: FontWeight.bold)
      ),
      SizedBox(width: XfgSize().size10),
    ]);
    return buttons;
  }

  /// 查看物流
  /// [isShipLogistics] 是否发货物流
  Future _checkLogistics(bool isShipLogistics) async {
    final expressDetail = isShipLogistics ? order.express?.expressDetail:order.refund?.express?.expressDetail;
    if (expressDetail != null) {
      XfgNav.pushNamed(Routes.myOrderRecoverLogistics, arguments: {
        'isShip': isShipLogistics, 'weight': order.weight?? 0, 'express': expressDetail});
    } else {
      '物流信息异常'.info();
    }
  }

  /// 通用信息
  Widget _itemWidget(String text, String value, {Color? valueColor}) => Container(
    padding: EdgeInsets.fromLTRB(XfgSize().size12, XfgSize().size14, XfgSize().size8, XfgSize().size14),
    child: Row(
      children: [
        XfgText(text, fontSize: XfgSize().text14, color: XfgColor.text1D),
        SizedBox(width: XfgSize().size24),
        Expanded(child: XfgText(value, fontSize: XfgSize().text14, color: valueColor?? XfgColor.text3, textAlign: TextAlign.end, height: 1.3))
      ],
    ),
  );

  /// 可复制的通用信息
  Widget _itemCopyWidget(String text, String? value) => Container(
    padding: EdgeInsets.fromLTRB(XfgSize().size12, XfgSize().size14, XfgSize().size8, XfgSize().size14),
    child: Row(
      children: [
        XfgText(text, fontSize: XfgSize().text14, color: XfgColor.text1D),
        Expanded(child: XfgText(value?? '--', fontSize: XfgSize().text14, color: XfgColor.text3, textAlign: TextAlign.end)),
        Visibility(
          visible: value?.isNotEmpty == true,
          child: Container(
            margin: EdgeInsets.only(left: XfgSize().size10),
            child: Builder(builder: (con) => GestureDetector(
              onTap: () {
                Clipboard.setData(ClipboardData(text: value?? '')).then((_) {
                  xfgShowSnackBar(con, text, value?? '');
                });
              },
              child: XfgText('复制', fontSize: XfgSize().text14, color: XfgColor.text1D),
            )),
          ),
        )
      ],
    ),
  );

  /// 按钮 装饰器
  ButtonStyle _buttonStyle(Color color) => ButtonStyle(
      backgroundColor: WidgetStatePropertyAll(color),
      shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(XfgSize().size40))),
      fixedSize: WidgetStatePropertyAll(Size(XfgSize().size97, XfgSize().size40))
  );
}
